#!/usr/bin/perl -w

use strict;
use warnings;
use utf8;
use open ':encoding(utf8)';
use lib '../';
use ImportHelpers;


my $sourceDir = ImportHelpers::getSourceDirName(__FILE__);
my $sourceURL = ImportHelpers::getSourceExternalURL($sourceDir);

my $stephWorkID = ImportHelpers::insertWork(
	id        => 6,
	language  => 'grc',
	osis_id   => 'Steph',
	name      => "Textus Receptus (1551)",
	type      => 'manuscript-edition',
	copyright => "By <a href='http://en.wikipedia.org/wiki/Desiderius_Erasmus' title='Desiderius Erasmus @ Wikipedia'>Desiderius Erasmus</a>. Edited by <a href='http://en.wikipedia.org/wiki/Robert_Estienne' title='Robert Estienne @ Wikipedia'>Robert Estienne</a>.",
	url       => $sourceURL,
	licenses  => [qw( http://creativecommons.org/licenses/publicdomain/ )]
);

my $scrivenerWorkID = ImportHelpers::insertWork(
	id        => 7,
	language  => 'grc',
	osis_id   => 'Scrivener',
	name      => "Textus Receptus (1894)",
	type      => 'manuscript-edition',
	copyright => "By <a href='http://en.wikipedia.org/wiki/Desiderius_Erasmus' title='Desiderius Erasmus @ Wikipedia'>Desiderius Erasmus</a>. Edited by <a href='http://en.wikipedia.org/wiki/Robert_Estienne' title='Robert Estienne @ Wikipedia'>Robert Estienne</a>, and <a href='http://en.wikipedia.org/wiki/Frederick_Henry_Ambrose_Scrivener' title='Frederick Henry Ambrose Scrivener @ Wikipedia'>F. H. A. Scrivener</a>.",
	url       => $sourceURL,
	licenses  => [qw( http://creativecommons.org/licenses/publicdomain/ )]
);


open IN, "$sourceDir/greek_textus_receptus_parsed_utf8.txt"
	or die "Dir greek_textus_receptus_parsed_utf8.txt doesn't exist.";

while(<IN>){
	#next if !/^62N\t/; #is 1 John
	next if not s/^(\d+\w)\t+(\d+)\t+(\d+)\t+\d+\t+//;
	
	my $book = $ImportHelpers::unboundNumToOsisCode{$1};
	next if !ImportHelpers::isBookOK($book);
	my $chapter = int $2;
	my $verse = int $3;
	my @tokens;
	
	chomp;
	s/\r//g;
	
	@tokens = ImportHelpers::parseUnboundVerseLine($_, 1);
	foreach my $token (@tokens){ #(split(/\r?\n/)){
		ImportHelpers::insertToken(
			work => $stephWorkID,
			
			data => $token->{data},
			type => $token->{type},
			
			book => $book,
			chapter => $chapter,
			verse => $verse,
			
			parsing => $token->{parse},
			strongs => join(';', @{$token->{strongs}})
		);
	}
	
	@tokens = ImportHelpers::parseUnboundVerseLine($_, 2);
	foreach my $token (@tokens){ #(split(/\r?\n/)){
		ImportHelpers::insertToken(
			work => $scrivenerWorkID,
			
			data => $token->{data},
			type => $token->{type},
			
			book => $book,
			chapter => $chapter,
			verse => $verse,
			
			parsing => $token->{parse},
			strongs => join(';', @{$token->{strongs}})
		);
	}
}
ImportHelpers::finishBookRefs($stephWorkID);
ImportHelpers::finishBookRefs($scrivenerWorkID);
close IN;


1;